import time
import numpy as np
from generator import generate_random_list
pop_list, size = generate_random_list()
def test_bubble_sort():
    """测试冒泡排序的正确性和性能"""
    print("="*50)
    print("开始测试冒泡排序算法")
    print("="*50)
    
    # 定义测试用例
    test_cases = [
        ("小型随机列表", 10, 0, 100),
        ("中型随机列表", 100, 0, 1000),
        ("大型随机列表", 1000, 0, 10000),
        ("已排序列表", 100, 0, 100, True),
        ("逆序列表", 100, 0, 100, False, True),
        ("重复元素列表", 50, 10, 20)  # 范围小会有很多重复
    ]
    
    total_tests = 0
    passed_tests = 0
    
    for name, size, low, high, *args in test_cases:
        total_tests += 1
        print(f"\n测试用例 {total_tests}: {name} (大小={size})")
        
        # 生成测试数据
        if args and args[0]:  # 已排序列表
            pop_list = sorted(np.random.randint(low, high, size=size).tolist())
        elif len(args) > 1 and args[1]:  # 逆序列表
            pop_list = sorted(np.random.randint(low, high, size=size).tolist(), reverse=True)
        else:  # 随机列表
            pop_list = np.random.randint(low, high, size=size).tolist()
        
        # 备份原始数据用于验证
        original = pop_list.copy()
        
        # 计时开始
        start_time = time.perf_counter()
        
        # 执行排序
        for i in range(size-1):
            for j in range(size-i-1):
                if pop_list[j] > pop_list[j+1]:
                    pop_list[j], pop_list[j+1] = pop_list[j+1], pop_list[j]
        
        # 计时结束
        elapsed = time.perf_counter() - start_time
        
        # 验证结果
        is_sorted = all(pop_list[i] <= pop_list[i+1] for i in range(len(pop_list)-1))
        is_correct = is_sorted and sorted(original) == pop_list
        
        if is_correct:
            passed_tests += 1
            result = "通过"
        else:
            result = "失败"
        
        # 打印结果
        print(f"结果: {result}")
        print(f"耗时: {elapsed:.6f}秒")
        if size <= 20:  # 小列表才打印内容
            print(f"原始数据: {original}")
            print(f"排序结果: {pop_list}")
    
    # 总结报告
    print("\n" + "="*50)
    print("测试总结")
    print("="*50)
    print(f"总测试用例: {total_tests}")
    print(f"通过测试: {passed_tests}")
    print(f"失败测试: {total_tests - passed_tests}")
    print(f"通过率: {passed_tests/total_tests*100:.2f}%")

if __name__ == "__main__":
    test_bubble_sort()